#include "threadPool.h"

static char *file_type(mode_t md)
{
	if (S_ISREG(md))
	{
		return "-";
	}
	else if (S_ISDIR(md))
	{
		return "d";
	}
	else if (S_ISFIFO(md))
	{
		return "p";
	}
	else
	{
		return "o";
	}
}
int myChdir(threadPool_t *pthreadPool)
{
	char dir[128] = "";
	printf("str = %s\n", pthreadPool->buf);
	printf("before cd,working directory: %s\n", getcwd(NULL, 0));
	int ret;
	ret = chdir(pthreadPool->buf); // 去/tmp
	if (-1 == ret)
	{
		perror("chdir");
		return -1;
	}
	getcwd(dir, 128);
	int len = strlen(dir);
	send(pthreadPool->netfd, &len, sizeof(int), 0);
	sendn(pthreadPool->netfd, dir, strlen(dir));
	printf("after cd,working directory: %s\n", getcwd(NULL, 0));
	return 0;
}
int pwd(threadPool_t *pthreadPool)
{
	bzero(pthreadPool->buf, BUF_SIZE);
	getcwd(pthreadPool->buf, BUF_SIZE);
	int len = strlen(pthreadPool->buf);
	send(pthreadPool->netfd, &len, sizeof(int), 0);
	sendn(pthreadPool->netfd, pthreadPool->buf, len);
	return 0;
}
int ls(threadPool_t *pthreadPool)
{
	DIR *pdir = opendir("./");
	if (pdir == NULL)
	{
		int flag = -1;
		send(pthreadPool->netfd, &flag, sizeof(int), 0);
	}
	else
	{
		struct dirent *mydir;
		int len;
		while ((mydir = readdir(pdir)) != NULL) // 读取目录
		{
			if (strncmp(mydir->d_name, ".", 1) == 0 || strncmp(mydir->d_name, "..", 2) == 0)
			{
				continue;
			}
			struct stat mystat;
			bzero(&mystat, sizeof(stat));
			stat(mydir->d_name, &mystat); // 获取某个文件的信息
			bzero(pthreadPool->buf, 1024);
			sprintf(pthreadPool->buf, "%-2s%-20s %10ldB", file_type(mystat.st_mode), mydir->d_name, mystat.st_size);
			len = strlen(pthreadPool->buf);
			send(pthreadPool->netfd, &len, sizeof(int), 0); // 发送数据给客户端
			sendn(pthreadPool->netfd, pthreadPool->buf, len);
		}
		len = 0;
		send(pthreadPool->netfd, &len, sizeof(int), 0);
	}
}
int reMove(threadPool_t *pthreadPool)
{
	char buf[128]="";
	int len;
	if (unlink(pthreadPool->buf) == -1)
	{
		perror("Failed to delete file");
		strcpy(buf, "Failed to delete file\n");
		len=strlen(buf);
		send(pthreadPool->netfd, &len, sizeof(int), 0);
		sendn(pthreadPool->netfd,buf,len);
	}
	else
	{
		strcpy(buf, "file removed success\n");
		len=strlen(buf);
		send(pthreadPool->netfd, &len, sizeof(int), 0);
		sendn(pthreadPool->netfd,buf,len);
	}
}

int recvn(int netfd, void *buf, int length)
{
	int total = 0; // 存储已经读取了多少个字节
	int ret;
	char *p = (char *)buf; // 一个char一个字节
	while (total < length)
	{
		ret = recv(netfd, p + total, length - total, 0);
		total += ret;
	}
}
int sendn(int fd_send, char *send_buf, int len)
{
	int sum = 0;
	int nsend;
	while (sum < len)
	{
		nsend = send(fd_send, send_buf + sum, len - sum, 0);
		sum += nsend;
	}
	return sum;
}
void getSalt(char *salt, char *passwd)
{
	int i, j;
	// 取出salt,i记录密码字符下标,j记录$出现次数
	for (i = 0, j = 0; passwd[i] && j != 3; ++i)
	{
		if (passwd[i] == '$')
			++j;
	}
	strncpy(salt, passwd, i - 1);
}
char* current_time() {
    time_t now = time(NULL);
    return ctime(&now);
}
void log_INFO(char* msg){
	syslog(LOG_INFO, "[%s] %s", current_time(), msg);
}
void log_ERROR(char* msg){
	syslog(LOG_INFO, "[%s] %s", current_time(), msg);
}
int md5(char *str,unsigned char *md){
    int fd = open(str, O_RDWR);
    MD5_CTX c;
    MD5_Init(&c);
    char buf[4096];
    while(1){
        bzero(buf,4096);
        ssize_t ret = read(fd,buf,4096);
        if(ret == 0){
            break;
        }
        MD5_Update(&c,buf,ret);
    }
    unsigned char MD[16]={0};
    MD5_Final(MD,&c);
	memcpy(md, MD, sizeof(MD));
    for(int i = 0; i < 16;++i){
        printf("%02x", MD[i]);
    }
    printf("\n");
	return 0;
}